<?php

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
$exc = new exchange($ecs->table("cardNum"), $db, 'card_id', 'card_name');

/*------------------------------------------------------ */
//-- 包装列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    assign_query_info();
    $smarty->assign('ur_here',     "卡号列表");
    $smarty->assign('action_link', array('text' => "添加卡号", 'href' => 'cardNum.php?act=add'));
    $smarty->assign('full_page',   1);

    $cardNums_list = cardNums_list();

    $smarty->assign('cardNum_list',    $cardNums_list['cardNum_list']);
    $smarty->assign('filter',       $cardNums_list['filter']);
    $smarty->assign('record_count', $cardNums_list['record_count']);
    $smarty->assign('page_count',   $cardNums_list['page_count']);

    $smarty->display('cardNum_list.htm');
}

/*------------------------------------------------------ */
//-- ajax列表
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $cardNums_list = cardNums_list();
    $smarty->assign('cardNum_list', $cardNums_list['cardNum_list']);
    $smarty->assign('filter',       $cardNums_list['filter']);
    $smarty->assign('record_count', $cardNums_list['record_count']);
    $smarty->assign('page_count',   $cardNums_list['page_count']);

	$sort_flag  = sort_flag($cardNums_list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('cardNum_list.htm'), '', array('filter' => $cardNums_list['filter'], 'page_count' => $cardNums_list['page_count']));
}
/*------------------------------------------------------ */
//-- 删除
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
    /* 检查权限 */
    check_authz_json('cardNum_manage');

    $cardNum_id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);

    $name = $exc->get_name($cardNum_id);
    if ($exc->drop($cardNum_id))
    {
        admin_log(addslashes($name),'remove','cardNum');

        $url = 'cardNum.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

        ecs_header("Location: $url\n");
        exit;
    }
    else
    {
        make_json_error($db->error());
    }
}
/*------------------------------------------------------ */
//-- 添加新包装
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'add')
{
    /* 权限判断 */
    admin_priv('cardNum_manage');

    /*初始化显示*/
    $cardNum['card_fee']   = 2000;

    $smarty->assign('cardNum',        $cardNum);
    $smarty->assign('ur_here',     "添加卡号");
    $smarty->assign('action_link', array('text' => "卡号列表", 'href' => 'cardNum.php?act=list'));
    $smarty->assign('form_action', 'insert');

    assign_query_info();
    $smarty->display('cardNum_info.htm');

}
elseif ($_REQUEST['act'] == 'insert')
{
    /* 权限判断 */
    admin_priv('cardNum_manage');

	if($_POST['rg_radio'] == 2){
		$start_cardNum = isset($_POST['start_card_name']) ? $_POST['start_card_name'] : 0;
		$end_cardNum = isset($_POST['end_card_name']) ? $_POST['end_card_name'] : 0;
		if (strlen($start_cardNum) != 9 || strlen($end_cardNum) != 9) sys_msg("卡号必须是9位数", 1);

		for($x = $start_cardNum; $x <= $end_cardNum; $x++){
			$is_only = $exc->is_only('card_name', $x);
			if (!$is_only) continue;

			$db->query("INSERT INTO ".$ecs->table('cardNum')." (card_name, card_fee) VALUES ('$x', '$_POST[card_fee]')");
		}
	}else{
		$is_only = $exc->is_only('card_name', $_POST['card_name']);
		if (!$is_only) sys_msg(sprintf("卡号 %s 已经存在", stripslashes($_POST['card_name'])), 1);

		$db->query("INSERT INTO ".$ecs->table('cardNum')." (card_name, card_fee) VALUES ('$_POST[card_name]', '$_POST[card_fee]')");
	}
    admin_log($_POST['card_name'],'add','cardNum');

    /*添加链接*/
    $link[0]['text'] = '继续添加新卡号';
    $link[0]['href'] = 'cardNum.php?act=add';

    $link[1]['text'] = '返回卡号列表';
    $link[1]['href'] = 'cardNum.php?act=list';

    sys_msg($_POST['card_name'].'已成功添加',0, $link);
}

/*------------------------------------------------------ */
//-- 编辑包装
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit')
{
    /* 权限判断 */
    admin_priv('cardNum_manage');

    $sql = "SELECT * FROM " .$ecs->table('cardNum'). " WHERE card_id='$_REQUEST[id]'";
    $cardNum = $db->GetRow($sql);

    $smarty->assign('ur_here',     '编辑卡号');
    $smarty->assign('action_link', array('text' => '卡号列表', 'href' => 'cardNum.php?act=list&' . list_link_postfix()));
    $smarty->assign('cardNum',        $cardNum);
    $smarty->assign('form_action', 'update');

    assign_query_info();
    $smarty->display('cardNum_info.htm');
}
elseif ($_REQUEST['act'] == 'update')
{
    /* 权限判断 */
    admin_priv('cardNum_manage');

    if ($_POST['card_name'] != $_POST['old_cardname']){
		if (strlen($_POST['card_name']) != 9) sys_msg("卡号必须是9位数", 1);
        /*检查品牌名是否相同*/
        $is_only = $exc->is_only('card_name', $_POST['card_name'], $_POST['id']);
        if (!$is_only) sys_msg(sprintf("卡号 %s 已经存在", stripslashes($_POST['card_name'])), 1); 
    }
    $param = "card_name = '$_POST[card_name]', card_fee = '$_POST[card_fee]'";

    if ($exc->edit($param,  $_POST['id']))
    {
        admin_log($_POST['card_name'], 'edit', 'cardNum');

        $link[0]['text'] = $_LANG['back_list'];
        $link[0]['href'] = 'cardNum.php?act=list&' . list_link_postfix();

        $note = sprintf('卡号 %s 修改成功', $_POST['card_name']);
        sys_msg($note, 0, $link);
    }
    else
    {
        die($db->error());
    }
}
/*------------------------------------------------------ */
//-- ajax编辑卡片名字
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_card_name')
{
    check_authz_json('cardNum_manage');
    $card_id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);
    $card_name = empty($_REQUEST['val']) ? '' : json_str_iconv(trim($_REQUEST['val']));

    if (!$exc->is_only('card_name', $card_name, $card_id))
    {
        make_json_error(sprintf("卡号 %s 已经存在", $card_name));
    }
    $old_card_name = $exc->get_name($card_id);
    if ($exc->edit("card_name='$card_name'", $card_id))
    {
        admin_log(addslashes($old_card_name), 'edit', 'cardNum');
        make_json_result(stripcslashes($card_name));
    }
    else
    {
        make_json_error($db->error());
    }
}
/*------------------------------------------------------ */
//-- ajax编辑卡片费用
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_card_fee')
{
    check_authz_json('cardNum_manage');
    $card_id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);
    $card_fee = empty($_REQUEST['val']) ? 0.00 : floatval($_REQUEST['val']);

    $card_name = $exc->get_name($card_id);
    if ($exc->edit("card_fee ='$card_fee'", $card_id))
    {
        admin_log(addslashes($card_name), 'edit', 'card');
        make_json_result($card_fee);
    }
    else
    {
        make_json_error($db->error());
    }
}


elseif ($_REQUEST['act'] == 'load_user'){
	$rank_id = $_REQUEST['rank_id'];
	$reg_field_id = $rank_id == 3 ? 17 : 7;

	$user_list = $GLOBALS['db']->getAll('SELECT user_id, user_name FROM ' . $GLOBALS['ecs']->table('users') . " WHERE user_rank = '".$rank_id."' ");
	foreach($user_list as $_k=>$_v){
		$reg_username = $GLOBALS['db']->getOne("SELECT content FROM " .$GLOBALS['ecs']->table('reg_extend_info')." WHERE user_id = '".$_v['user_id']."' and reg_field_id = '".$reg_field_id."'");
		$user_list[$_k]['user_name'] = $reg_username ? $reg_username : $_v['user_name'];
	}
	die(json_encode($user_list));
}

elseif ($_REQUEST['act'] == 'batch'){
	$link[] = array('href' => 'cardNum.php?act=list', 'text' => '卡号列表');

	$card_id = !empty($_POST['checkboxes']) ? join(',', $_POST['checkboxes']) : 0;

	if (isset($_POST['type'])) {
		admin_priv('cardNum_manage');
		if ($_POST['type'] == 'move_tosales'){
			$record_count = $GLOBALS['db']->getOne("select count(*) from ".$GLOBALS['ecs']->table('cardNum')." WHERE sales_id = '".$_POST['target_cat']."' and card_id in($card_id)");
			if (!$record_count){
				$exc->edit("sales_id ='".$_POST['target_cat']."'", $card_id);
			}else{
				sys_msg("你所选的选项中有已经绑定的业务员！", 0, $link);
			}
        } elseif ($_POST['type'] == 'move_toissuing') {
			$record_count = $GLOBALS['db']->getOne("select count(*) from ".$GLOBALS['ecs']->table('cardNum')." WHERE issuing_id = '".$_POST['target_cat']."' and card_id in($card_id)");
			if (!$record_count){
				$exc->edit("issuing_id ='".$_POST['target_cat']."'", $card_id);
			}else{
				sys_msg("你所选的选项中有已经绑定的发卡人！", 0, $link);
			}
        } elseif ($_POST['type'] == 'trash') {
			$GLOBALS['db']->query("DELETE FROM ".$GLOBALS['ecs']->table('cardNum')." WHERE card_id in($card_id)");
		}
	}
	/* 清除缓存 */
    clear_cache_files();
	
	sys_msg("批量操作成功！", 0, $link);
}

function cardNums_list()
{
    $result = get_filter();
    if ($result === false)
    {
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'card_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

        /* 分页大小 */
        $sql = "SELECT count(*) FROM " .$GLOBALS['ecs']->table('cardNum');
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        $filter = page_and_size($filter);

        /* 查询 */
        $sql = "SELECT * ".
               " FROM ".$GLOBALS['ecs']->table('cardNum').
               " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'] .
               " LIMIT " . $filter['start'] . ',' . $filter['page_size'];

        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $cardNum_list = $GLOBALS['db']->getAll($sql);
	
	foreach($cardNum_list as $_k=>$_v){
		$cardNum_list[$_k]['sales_name'] = $GLOBALS['db']->getOne("SELECT content FROM " .$GLOBALS['ecs']->table('reg_extend_info')." WHERE user_id = '".$_v['sales_id']."' and reg_field_id =7");
		$cardNum_list[$_k]['issuing_name'] = $GLOBALS['db']->getOne("SELECT content FROM " .$GLOBALS['ecs']->table('reg_extend_info')." WHERE user_id = '".$_v['issuing_id']."' and reg_field_id =17");
		$cardNum_list[$_k]['user_name'] = $GLOBALS['db']->getOne("SELECT content FROM " .$GLOBALS['ecs']->table('reg_extend_info')." WHERE user_id = '".$_v['user_id']."' and reg_field_id =25");
	}

    $arr = array('cardNum_list' => $cardNum_list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

    return $arr;
}

?>